package cn.hct.springboot_pro.controller;

import cn.hct.springboot_pro.entity.Item;
import cn.hct.springboot_pro.entity.Orders;
import cn.hct.springboot_pro.entity.User;
import cn.hct.springboot_pro.service.ItemService;
import cn.hct.springboot_pro.service.OrderService;
import cn.hct.springboot_pro.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import sun.plugin.dom.core.Element;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@Controller
@RequestMapping("/User")
public class UserController {

    @Autowired
    UserService userService;
    @Autowired
    OrderService orderService;
    @Autowired
    ItemService itemService;

    @RequestMapping("/loginCheck")
    public String loginCheck(Model model, HttpSession session, User user, HttpServletResponse response) throws IOException {
        String uname = user.getUser_name();
        String psw =user.getPassword();
        User userLogin=userService.getUserByUnameAndPsw(uname,psw);

        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html; charset=utf-8");
        PrintWriter out = response.getWriter();
        if (userLogin !=null ){
            int user_id=userLogin.getUser_id();
            if (userLogin.getManager()==1){
                session.setAttribute("user_name",uname);
                session.setAttribute("user_id",user_id);
                return "redirect:/Item/user-index";
            }
            else {
                session.setAttribute("admin_name", uname);
                session.setAttribute("admin_id",user_id);
                return "redirect:/User/admin-index";
            }
        }else {

            out.print("<script>alert('" + "用户名或密码错误" + "');history.go(-1);</script>");
            return null;
        }
    }

    @RequestMapping("/register")
    public String register(User user,HttpServletResponse response) throws IOException{
        String uname=user.getUser_name();
        User userCheck=userService.getUserByUname(uname);

        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html; charset=utf-8");
        PrintWriter out = response.getWriter();

        if (userCheck != null){
            out.print("<script>alert('" + "该用户名已被注册!请重新填写噢" + "');history.go(-1);</script>");
            return null;
        }else{
            int adminReg=userService.insertUser(uname,user.getPhone(),user.getPassword());
            if (adminReg !=0){
               /* model.addAttribute("message","恭喜你注册成功");
                return "message";*/
                out.print("<script>alert('" + "恭喜你注册成功，快快登录吧" + "');window.location.href='/Home/index';</script>");
                return null;
            }else{
                out.print("<script>alert('" + "不知道为什么注册不成功，打电话问我吧：8888-8888" + "');history.go(-1);</script>");
                return null;
            }
        }
    }

    @RequestMapping("/user-news")
    public String userNews(Model model,HttpSession session){
        int user_id= (int) session.getAttribute("user_id");
        User userNews = userService.getUserById(user_id);
        model.addAttribute("userNews",userNews);
        List<Orders> ordersList = orderService.getOrderByUId(user_id);
        model.addAttribute("ordersList",ordersList);
        List<Item> itemList = itemService.getItemByUid(user_id);
        model.addAttribute("itemList",itemList);
        return "/user/user-news";
    }


    //注销功能
    @RequestMapping("/outLogin")
    public String outLogin(HttpSession session){
        //通过session.invalidata()方法来注销当前的session
        session.invalidate();
        return "login";
    }

    //更新用户信息
    @RequestMapping("/updateUser")
    public  String  update(Model model,User user,HttpServletResponse response) throws IOException {
        //更新信息
        int updateUser = userService.updateUser(user);
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html; charset=utf-8");
        PrintWriter out = response.getWriter();

        if (updateUser !=0){
            out.print("<script>alert('" + "更改信息成功" + "');window.location.href='/User/user-news';</script>");
            return null;
        }else{
            model.addAttribute("message","不知道更改不成功，打电话问我吧：8888-8888");
            return "public/message";
        }
    }

    @RequestMapping("admin-index")
    public String adminIndex(HttpSession session,Model model){

        int user_id= (int) session.getAttribute("admin_id");
        /*用户列表*/
        List<User> userList=userService.getUserByMan(user_id);
        model.addAttribute("userList",userList);
        /*商品列表*/
        List<Item> itemList = itemService.getItem();
        model.addAttribute("itemList",itemList);
        /*订单列表*/
        List<Orders> ordersList = orderService.getOrder();
        model.addAttribute("ordersList",ordersList);
        return "admin/admin-index";
    }

    @RequestMapping("/deleteUser")
    public String deleteUser(Model model,int user_id,HttpServletResponse response) throws IOException {
        int i = userService.deleteUserById(user_id);
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html; charset=utf-8");
        PrintWriter out = response.getWriter();
        if (i!=0){
            out.print("<script>alert('" + "删除用户成功" + "');window.location.href='/User/admin-index';</script>");
            return null;
        }
        else{
            model.addAttribute("message","删除失败，找工作人员维修吧！！！");
            return "public/message";
        }


    }
}
